<?php
	include_once("./global.php");
	session_start();
	if($_SESSION[id]){
	
		if($_POST['collect_app']){
/*				$term = 'term='.$_POST['itunes_term'];
				$country = "&country=cn&";
				$entity = 'entity='.$_POST['collect_itunes_entity'];
				$limit = '&limit='.$_POST['itunes_limit'];
				$url = "https://itunes.apple.com/search?";
				$ios = $url.$term.$country.$entity.$limit;
				/*echo $ios;
				$json_str = file_get_contents($ios);*/
				$id = $_POST['itunes_term'];
				$url = "https://itunes.apple.com/cn/app/id".$id;
				$str = file_get_contents($url);
				$keyword = "/<\/span><a href=\"(.*)\">(.*)<\/a>/Usi";
				$price = "/<ul class=\"list\"><li><div class=\"price\">(.*)<\/div><\/li>/Usi";
				$list = "/<li><span class=\"label\">(.*)<\/span>(.*)<\/li>/Usi";
				$language = "/<li class=\"language\"><span class=\"label\">(.*)<\/span>(.*)<\/li>/Usi";
				$date = "/<li class=\"release-date\"><span class=\"label\">(.*)<\/span>(.*)<\/li>/Usi";
				$icon_175 = "/<img width=\"175\" height=\"175\" alt=\"(.*)\" class=\"(.*)\" src=\"(.*)\" \/>/Usi";
				$screenshot = "/<div class=\"lockup\"><img alt=\"(.*)\"(.*)src=\"(.*)\" \/><\/div>/Usi";
				$meta = "/<meta name=\"keywords\" content=\"(.*)\" \/>/Usi";
				$desc = "/<h4>(.*)<\/h4><div>(.*)<\/div><div class=\"rating\"/Usi";
				$rating = "/<\/span><\/div><span class=\"rating-count\">(.*)<\/span><\/div><\/div>/Usi";
				$system = "/<p><span class=\"app-requirements\">(.*)<\/span>(.*)<\/p>/Usi";
				
				preg_match_all($keyword,$str,$keyword_arr);
				preg_match_all($price,$str,$price_arr);
				preg_match_all($list,$str,$list_arr);
				preg_match_all($date,$str,$date_arr);
				preg_match_all($language,$str,$language_arr);
				preg_match_all($icon_175,$str,$icon_arr);
				preg_match_all($desc,$str,$desc_arr);
				preg_match_all($screenshot,$str,$screen_arr);
				preg_match_all($meta,$str,$meta_arr);
				preg_match_all($desc,$str,$desc_arr);
				preg_match_all($rating,$str,$rating_arr);
				preg_match_all($system,$str,$system_arr);
				
				$rating_str = $rating_arr[1][0];
				echo $rating_str;
				$keyword = $meta_arr[1][0];
				$keyword_explode = explode(",",$keyword);
				$count_keyword = count($keyword_explode);
				for($m=0;$m<$count_keyword;$m++){
					$check_en = $keyword_explode[$m];
					$check = preg_match("/^[^\x80-\xff]+$/",$check_en);
					if(!$check){
						echo $keyword_explode[$m];
					}else{
						continue;
					}
				}
				$A_Appicon = $icon_arr[3][0];
				$A_Apptrackname = $icon_arr[1][0];
				$A_Applanguage = $language_arr[2][0];
				$A_Appreleasedate = $date_arr[2][0];
				$A_Appprice = $price_arr[2][0];
				$A_Category = $keyword_arr[2][0];
				$A_Appversion = $list_arr[2][0];
				$A_Appfilesize = $list_arr[2][1];
				$A_Appartistname = $list_arr[2][2];	
				$A_Appdesc = $desc_arr[4][0];	
				$A_Appsystem = $system_arr[2][0];
				print_r($desc_arr);	
				$count = count($screen_arr[3]);
				$count = $count/2;
				for($i=0;$i<$count;$i++){
					$screen[] = array("id"=>$i,"url"=>$screen_arr[3][$i]);
				}
				print_r($screen);
				
		}
		if(isset($_POST['insert_app'])){
			print_r($_POST);
		}
		
/*		if(isset($_POST['collect_apps'])){
			$term = 'term='.$_POST['itunes_term'];
			$country = "&country=cn&";
			$entity = 'entity='.$_POST['collect_itunes_entity'];
			$limit = '&limit='.$_POST['itunes_limit'];
			$url = "https://itunes.apple.com/search?";
			$ios = $url.$term.$country.$entity.$limit;
			echo $ios;
			$json_str = file_get_contents($ios);
		}*/
		
		
		
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>后台首页</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<style>
	body{ background:#e7e7e7; margin:0; padding:0;}
	#container{ width:980px; height:auto; position:relative; margin:0 auto;}
	#container #header{ width:980px; height:90px; position:relative; background:#000; line-height:90px;color:#fff; font-size:24px; text-align:center;}
	.left{width:250px; height:auto; position:relative; float:left;}
	.left table{width:248px; height:auto; position:relative; float:left; background:#fff;}
	.left table td{ height:20px; text-align:center;}
	.left table .selected{ background:#73abe3;}
	#content{ width:730px; height:100%; position:relative; float:left; margin:2px 0px;}
	#content .title{ width:720px; height:25px; position:relative; float:left; line-height:25px; font-size:18px; color:#fff; background:#000;}
	form{margin:20px auto; width:720px; height:auto; position:relative; float:left;}
	form .inpuarea{ width:360px; height:150px;}
</style>
</head>
<body>
	<div id="container">
		<div id="header">
			818苹果站后台管理
		</div>
		<div class="left">
			<table>
				<tr><td style="background:#000000; color:#fff;">站点管理</td></tr>
				<tr><td><a href="admin_site_mangerment.php">全局管理</a></td></tr>
				<tr><td>静态页面管理</td></tr>
				<tr><td><a href="admin_link_mangerment.php?page=1">友情链接管理</a></td></tr>
				<tr><td><a href="admin_member_mangerment.php?page=1">会员管理</a></td></tr>
				<tr><td><a href="admin_tags_mangerment.php?page=1">标签管理</a></td></tr>
				<tr><td><a href="admin_search_mangerment.php">搜索管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">文章管理</td></tr>
				<tr><td><a href="admin_category_mangerment.php">文章分类管理</a></td></tr>
				<tr><td><a href="admin_news_add.php">发布文章</a></td></tr>
				<tr><td><a href="admin_news_mangerment.php?page=1">文章管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">应用管理</td></tr>
				<tr><td><a href="admin_appcate_mangerment.php">应用分类管理</a></td></tr>
				<tr><td><a href="admin_app_add.php">添加应用</a></td></tr>
				<tr><td><a href="admin_app_mangerment.php?page=1">应用管理</a></td></tr>
                <tr><td><a href="admin_app_collection.php">应用采集</a></td></tr>
				<tr><td class="selected"><a href="damin_app_collcetion.php">单应用采集</a></td></tr>
			</table>
		</div>
        <div id="content">
			<div class="title">应用采集</div>
            	<table>
				<form action="admin_app_collection_edit.php" method="post">
                <tr>
                	<td>输入APPID:</td>
                    <td><input type="text" name="itunes_term" placeholder="game,weather等" /></td>
                </tr>
                <tr>
                	<td></td>
                    <td><input type="submit" name="collect_app" value="采集APP" /></td>
                </tr>
                </form>
                </table>
		</div>
		
        <table style=" width:700px; border:1px solid #000">
        	<tr>
            	<td style="border:1px solid #000">APPId</td>
                <td style="border:1px solid #000">APPName</td>
                <td style="border:1px solid #000">类别ID</td>
                <td style="border:1px solid #000">描述</td>
                <td style="border:1px solid #000">APP厂商</td>
                <td style="border:1px solid #000">APP价格</td>
<!--                <td style="border:1px solid #000">APP评分</td>
                <td style="border:1px solid #000">评分人数</td>-->
                <td style="border:1px solid #000">关键词</td>
                <td style="border:1px solid #000">下载地址</td>
                <td style="border:1px solid #000">操作</td>
            </tr>            
        	<?php
        		if($json_str){
					$json_str;
					$json = json_decode($json_str);
					$total = $json->resultCount;
					$i = '0';
					for($i=0;$i<$total;$i++){
						$json_test = $json->results[$i];
						$array = (array)$json_test;
			?>
            <form name="form1" action="admin_app_collection_edit.php" method="post">
        		<tr>
                	<td style=" width:50px; border:1px solid #000">
						<input type="checkbox" name="insert_trackid[]" value="<?php $app_trackId = $array['trackId']; echo $app_trackId;?>" />
						<?php echo $app_trackId;?>
						</td>
                    <td style="width:50px;border:1px solid #000">
						<input type="checkbox" value="<?php $app_trackName = $array['trackName']; echo $app_trackName?>" name="insert_trackname" />
						<?php 
							echo $app_trackName; 
						?>
                    </td>
                    <td style="width:50px;border:1px solid #000">
					<?php 
						$primaryGenreId = $array['primaryGenreId'];
						if(!$primarygenreId){
							echo "采集不到类别，请在进入编辑页面后根据应用介绍等信息自行添加";
						}
						else if($primarygenreId){
						$categoryname = $array['genres'][0];
						$sql_pgi = "select * from keywordinfo where K_ID = '$primaryGenreId'";
						$query_pgi = $mysqli->query($sql_pgi);
						$result_pgi = $query_pgi->fetch_assoc();
						if($result_pgi){
							$app_primaryGenreId = $result_pgi['K_KID'];
							$categoryname = $result_pgi['K_Name'];
							$sql_category = "select AC_Categoryid from appcategoryinfo where AC_Categoryname = '$categoryname'";
							$query_category = $mysqli->query($sql_category);
							$result_category = $query_category->fetch_assoc();
							if($result_category){
								$app_primaryGenreId = $result_category['AC_Categoryid'];
						?>
							<input type="checkbox" value="<?php echo $app_primaryGenreId;?>" name="insert_appcategoryid[]" /><?php echo $app_primaryGenreId;?>
						<?php
							}else{
								$sql_insert_category = "insert into appcategoryinfo (AC_Categoryid,AC_Categoryname) value(NULL,'$categoryname')";
								$query_insert_category = $mysqli->query($sql_insert_category);
								if($query_insert_category){
									echo $categoryname."-插入成功"."<br />";
									$sql_aci = "select AC_Categoryid from appcategoryinfo where AC_Categoryname = '$categoryname'";
									$query_aci = $mysqli->query($sql_aci);
									$result_aci = $query_aci->fetch_assoc();
									$app_primaryGenreID = $result_aci['AC_Categoryid'];
						?>
							<input type="checkbox" value="<?php echo $app_primaryGenreId;?>" name="insert_appcategoryid[]" /><?php echo $app_primaryGenreId;?>
						<?php
								}else{
									echo "查询失败";
								}
							}
						}else{
							$sql_insert_keyword = "insert into keywordinfo (K_KID,K_ID,K_Name,K_search,K_tags) values (NULL,'$primaryGenreId','$categoryname',NULL,'0')";
							$query_insert_keyword = $mysqli->query($query_insert_keyword);
							$sql_insert_category = "insert into appcategoryinfo (AC_Categoryid,AC_Categoryname) values (NULL, '$categoryname')";
						}
					}
					?>
					</td>
                   	<td style="width:160px;border:1px solid #000">
                    <input type="checkbox" name="insert_appdesc" value="
					<?php 
						$app_description = $array['description']; 
						echo $app_description;				
					?>" />
					</td>
                    <td style="border:1px solid #000;">
                    	<input type="checkbox" name="inser_atristname" value="<?php echo $array['artistName']?>" />
                    </td>
                    <td style="width:50px;border:1px solid #000">
					<?php 
						$app_formattedPrice = $array['formattedPrice'];
					?>
						<input type="checkbox" value="<?php echo $app_formattedPrice;?>" name="insert_appprice" /> <?php echo $app_formattedPrice;?>
                    </td>
                    <td style="width:160px;border:1px solid #000"><input type="checkbox" name="insert_keyword[]" value="
              		<?php
						$genres = $array['genres'];
						$genres_i = $array['genreIds'];
						$count_genres = count($genres);
						$count_genres_i = count($genres_i);
							for($j='0';$j<$count_genres;$j++){
								$genres_arr[$j] = array(
														id => $genres_i[$j],
														name => $genres[$j]
														);								
								$k_id = $genres_arr[$j]['id'];
								$k_name = $genres_arr[$j]['name'];	
								$sql_select = "select K_KID from keywordinfo where K_ID = '$k_id'";
								$query_select = $mysqli->query($sql_select);
								$result_select = $query_select->fetch_assoc();
								if($result_select){
									$sql_select_kn = "select K_Name from keywordinfo where K_KID = '$result_select[K_KID]'";
									$query_select_kn = $mysqli->query($sql_select_kn);
									$result_select_kn = $query_select_kn->fetch_assoc();
									if($result_select_kn){
										$tip = 'ID和NAME存在';
										$sql_select_kid = "select K_KID from keywordinfo where K_ID = '$k_id'";
										$query_select_kid = $mysqli->query($sql_select_kid);
										while($result_select_kid = $query_select_kid->fetch_assoc()){							
											$kid = $result_select_kid['K_KID'];
										}
									}else{
										$sql_update_kn = "update keywordinfo set K_Name = '$k_name' where K_ID = '$k_id'";
										$query_update_kn = $mysqli->query($sql_update_kn);
										if($query_update_kn){
											$tip = "保存成功";
											$sql_select_kid = "select K_KID from keywordinfo where K_ID = '$k_id'";
											$query_select_kid = $mysqli->query($sql_select_kid);
											while($result_select_kid = $query_select_kid->fetch_assoc()){
												$kid = $result_select_kid['K_KID'];
											}
										}else{
											$tip = $mysqli->error."保存失败";
										}
									}									
								}else{
									$sql_select_kid = "select K_ID from keywordinfo where K_Name = '$k_name'";
									$query_select_kid = $mysqli->query($sql_select_kid);
									$result_select_kid = $query_select_kid->fetch_assoc();
									if($result_select_kid){
										$sql_update_kid = "update keywordinfo set K_KID = '$k_id' where K_Name = '$k_name'";
										$query_update_kid = $mysqli->query($sql_update_kid);
										if($query_update_kid){
											$tip = "更新成功";
											$sql_select_kid = "select K_KID from keywordinfo where K_ID = '$k_id'";
											$query_select_kid = $mysqli->query($sql_select_kid);
											while($result_select_kid = $query_select_kid->fetch_assoc()){
												$kid = $result_select_kid['K_KID'];
											}
										}else{
											$tip = $mysqli->error."更新失败";
										}
									}else{
										$sql_insert_kid = "insert into keywordinfo (K_KID,K_ID,K_Name) values(NULL,'$k_id','$k_name')";
										$query_insert_kid = $mysqli->query($sql_insert_kid);
										if($query_insert_kid){
											$tip = "插入成功";
											$sql_select_kid = "select K_KID from keywordinfo where K_ID = '$k_id'";
											$query_select_kid = $mysqli->query($sql_select_kid);
											while($result_select_kid = $query_select_kid->fetch_assoc()){
												$kid = $result_select_kid['K_KID'];
											}
										}else{
											$tip = $mysqli->error."插入失败";
										}
									}
								}
								$keywordid = $kid.",";
                        		echo $keywordid; 							
							}
						?>" />
					</td>
                    <td style="width:50px; border:1px solid #000"><input type="checkbox" name="insert_download" value="<?php
						$download = $array['trackViewUrl'];
						echo $download;
					?>" />
					</td>
                    <td style=" width:50px;border:1px solid #000">
						<input type="submit" name="insert_app" value="保存APP" />
					</td>
                </tr>
        	<?php
					}
				}else{
			?>
				<tr><td style="border:1px solid #000">出错</td></tr>
			<?php
				}
			?>            
        </table>
		</form>
    </div>
</body>
<script>
	function select_all(){
		var length = document.form1.elements.length;
		var i = 0;
		for(i=0;i<length;i++){
			var checked = document.form1.elements[i];
			if(checked.name != 'insert_trackid'){
				if(document.getElementById("insert_trackid").checked != false){
					checked.checked = true;
				}else{
					checked.checked = false;
				}
			}
		}
	}
	function del(){
		var length = document.form1.elements.length;
		var i = 0;
		for(i=0;i<length;i++){
			var checked = document.form1.elements[i];
			if(checked.name != 'insert_trackid'){
				if(checked.checked == true){
					alert(checked.name+checked.value);
				}
			}
		}
	}
</script>
</html>
<?php
	}else{
		echo "登录先";
		echo "<meta http-equiv=\"refresh\" content=\"1; url='admin_login.php' />";
	}

?>